package persentation.presentUniversityDean;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import vo.courseVO.CourseVO;
import vo.teachPlanVO.TeachPlanVO;
import businesslogic.courseBl.CourseController;
import businesslogic.teachPlanBl.TeachPlanController;

import javax.swing.JComboBox;
import javax.swing.JLabel;

public class YuanxijihuaPanel extends JPanel {
	private MyTabbedPane tabbedPane;
	private JTable table_1;
	private TeachPlanController teachPlanController;
	private CourseController courseController;
	private ArrayList<TeachPlanVO> teachPlanVOs;
	private ArrayList<TeachPlanVO> teachPlanVOs1;
	private ArrayList<TeachPlanVO> teachPlanVOs2;
	private ArrayList<TeachPlanVO> teachPlanVOs3;
	private String departmentID;
	private String[] attributeList = new String[4];
	private String[] semesterList = new String[9];
	private String attr, seme;

	/**
	 * Create the panel.
	 */
	public YuanxijihuaPanel(TeachPlanController teachPlanController,
			String departmentID) {
		this.teachPlanController = teachPlanController;
		this.departmentID = departmentID;
		courseController = new CourseController("jiaowulaoshi");
		// System.out.println("here*******************************");
		try {
			teachPlanVOs = teachPlanController
					.getPlanByDepartment(departmentID);
		} catch (Exception e2) {
			// TODO 自动生成的 catch 块
			e2.printStackTrace();
			ExceptionHandler.networkException();
		}
		teachPlanVOs1 = new ArrayList<TeachPlanVO>();
		teachPlanVOs2 = new ArrayList<TeachPlanVO>();
		teachPlanVOs3 = new ArrayList<TeachPlanVO>();

		attributeList[0] = "全部";
		attributeList[1] = "通识通修";
		attributeList[2] = "专业核心";
		attributeList[3] = "开放选修";

		semesterList[0] = "全部";
		semesterList[1] = "第一学期";
		semesterList[2] = "第二学期";
		semesterList[3] = "第三学期";
		semesterList[4] = "第四学期";
		semesterList[5] = "第五学期";
		semesterList[6] = "第六学期";
		semesterList[7] = "第七学期";
		semesterList[8] = "第八学期";

		for (int i = 0; i < teachPlanVOs.size(); i++) {
			String attribute = teachPlanVOs.get(i).getAttribute();
			System.out.println("+++++++++++++++++++++++++++++");
			System.out.println(attribute);
			if (attribute != null) {
				if (attribute.equals("通识通修")) {
					teachPlanVOs1.add(new TeachPlanVO(teachPlanVOs.get(i)
							.getCourseId(),
							teachPlanVOs.get(i).getCourseName(), teachPlanVOs
									.get(i).getCredit(), teachPlanVOs.get(i)
									.getDepartment(), teachPlanVOs.get(i)
									.getAttribute(), teachPlanVOs.get(i)
									.getSemester()));
				} else if (attribute.equals("专业核心")) {
					teachPlanVOs2.add(new TeachPlanVO(teachPlanVOs.get(i)
							.getCourseId(),
							teachPlanVOs.get(i).getCourseName(), teachPlanVOs
									.get(i).getCredit(), teachPlanVOs.get(i)
									.getDepartment(), teachPlanVOs.get(i)
									.getAttribute(), teachPlanVOs.get(i)
									.getSemester()));
				} else {
					teachPlanVOs3.add(new TeachPlanVO(teachPlanVOs.get(i)
							.getCourseId(),
							teachPlanVOs.get(i).getCourseName(), teachPlanVOs
									.get(i).getCredit(), teachPlanVOs.get(i)
									.getDepartment(), teachPlanVOs.get(i)
									.getAttribute(), teachPlanVOs.get(i)
									.getSemester()));
				}
			}
		}

		// System.out.println("here1212*******************************");
		setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(97, 90, 618, 302);
		add(scrollPane);

		table_1 = new JTable();
		table_1.setModel(new DefaultTableModel(new Object[][] {
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null },
				{ null, null, null, null, null }, }, new String[] {
				"\u8BFE\u7A0B\u53F7", "\u8BFE\u7A0B\u540D\u79F0",
				"\u5B66\u5206", "\u8BFE\u7A0B\u5C5E\u6027", "\u5B66\u671F" }) {
			Class[] columnTypes = new Class[] { Object.class, Object.class,
					Integer.class, Object.class, String.class };

			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}

			boolean[] columnEditables = new boolean[] { false, false, false,
					false, false };

			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table_1.getColumnModel().getColumn(0).setResizable(false);
		table_1.getColumnModel().getColumn(1).setResizable(false);
		table_1.getColumnModel().getColumn(2).setResizable(false);
		table_1.getColumnModel().getColumn(3).setResizable(false);
		table_1.getColumnModel().getColumn(4).setResizable(false);
		scrollPane.setViewportView(table_1);
		fillTable();

		JButton button = new JButton("查看详细信息");

		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				TeachPlanVO teachPlanVO = teachPlanVOs.get(table_1
						.getSelectedRow());
				CourseVO courseVO = null;
				try {
					courseVO = courseController.getCourse(teachPlanVO
							.getCourseId());
				} catch (Exception e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
					ExceptionHandler.networkException();
				}
				System.out.println(table_1.getSelectedRow());
				System.out.println(courseVO.getCourseName());

				tabbedPane.addOne(courseVO.getCourseName() + " 详细信息",
						new CourseInfoPanel(courseVO));

			}
		});

		button.setBounds(356, 420, 114, 35);
		add(button);
		attr = "全部";
		seme = "全部";
		final JComboBox comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					attr = attributeList[comboBox.getSelectedIndex()];
					changeAttributeList(
							attributeList[comboBox.getSelectedIndex()], seme);
				}
			}
		});
		comboBox.setModel(new DefaultComboBoxModel(attributeList));
		comboBox.setBounds(197, 48, 119, 26);
		add(comboBox);

		JLabel label = new JLabel("课程属性");
		label.setBounds(113, 48, 55, 26);
		add(label);

		JLabel label_1 = new JLabel("学期");
		label_1.setBounds(458, 48, 55, 26);
		add(label_1);

		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					seme= semesterList[comboBox_1.getSelectedIndex()];
					changeAttributeList(attr,
							semesterList[comboBox_1.getSelectedIndex()]);
				}
			}
		});
		comboBox_1.setModel(new DefaultComboBoxModel(semesterList));
		comboBox_1.setBounds(557, 48, 119, 26);
		add(comboBox_1);
	}

	public void fillTable() {
		DefaultTableModel tableModel = (DefaultTableModel) table_1.getModel();
		tableModel.setRowCount(0);
		if(teachPlanVOs!=null){
			
		
		for (TeachPlanVO teachPlan : teachPlanVOs) {
			String[] array = new String[5];
			array[0] = teachPlan.getCourseId();
			array[1] = teachPlan.getCourseName();
			array[2] = String.valueOf(teachPlan.getCredit());
			array[3] = teachPlan.getAttribute();
			array[4] = teachPlan.getSemester();

			System.out.println(array[0]);
			System.out.println(array[1]);
			System.out.println(array[2]);
			System.out.println(array[3]);
			tableModel.addRow(array);
			table_1.invalidate();
			table_1.updateUI();
		}
		}else{
			//JOptionPane.showMessageDialog(null, "！",
			//		"南京大学选课系统", JOptionPane.INFORMATION_MESSAGE);
		}
	}

	public void changeAttributeList(String attribute, String semester) {
		if (attribute.equals("全部")) {
			if (semester.equals("全部")) {
				try {
					teachPlanVOs = teachPlanController
							.getPlanByDepartment(departmentID);
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
					ExceptionHandler.networkException();
				}
			} else {
				try {
					teachPlanVOs = teachPlanController.getPlanBySemester(
							semester, departmentID);
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
					ExceptionHandler.networkException();
				}
			}
		} else if (attribute.equals("通识通修")) {
			if (semester.equals("全部")) {
			teachPlanVOs = teachPlanVOs1;
			}else{
				ArrayList<TeachPlanVO> teachPlanVOsT = new ArrayList<TeachPlanVO>();
				for(int i=0;i<teachPlanVOs1.size();i++){
					if(teachPlanVOs1.get(i).getSemester().equals(semester)){
					teachPlanVOsT.add(new TeachPlanVO(teachPlanVOs1.get(i)
								.getCourseId(),
								teachPlanVOs1.get(i).getCourseName(), teachPlanVOs1
										.get(i).getCredit(), teachPlanVOs1.get(i)
										.getDepartment(), teachPlanVOs1.get(i)
										.getAttribute(), teachPlanVOs1.get(i)
										.getSemester()));
					}
				}
				
				teachPlanVOs = teachPlanVOsT;
			}
		} else if (attribute.equals("专业核心")) {
			if (semester.equals("全部")) {
			teachPlanVOs = teachPlanVOs2;
			}else{
				ArrayList<TeachPlanVO> teachPlanVOsT =  new ArrayList<TeachPlanVO>();
				for(int i=0;i<teachPlanVOs2.size();i++){
					if(teachPlanVOs2.get(i).getSemester().equals(semester)){
					teachPlanVOsT.add(new TeachPlanVO(teachPlanVOs2.get(i)
								.getCourseId(),
								teachPlanVOs2.get(i).getCourseName(), teachPlanVOs2
										.get(i).getCredit(), teachPlanVOs2.get(i)
										.getDepartment(), teachPlanVOs2.get(i)
										.getAttribute(), teachPlanVOs2.get(i)
										.getSemester()));
					}
				}
				
				teachPlanVOs = teachPlanVOsT;
			}
		} else {
			if (semester.equals("全部")) {
			teachPlanVOs = teachPlanVOs3;
			}else{
				ArrayList<TeachPlanVO> teachPlanVOsT =  new ArrayList<TeachPlanVO>();
				for(int i=0;i<teachPlanVOs3.size();i++){
					if(teachPlanVOs3.get(i).getSemester().equals(semester)){
					teachPlanVOsT.add(new TeachPlanVO(teachPlanVOs3.get(i)
								.getCourseId(),
								teachPlanVOs3.get(i).getCourseName(), teachPlanVOs3
										.get(i).getCredit(), teachPlanVOs3.get(i)
										.getDepartment(), teachPlanVOs3.get(i)
										.getAttribute(), teachPlanVOs3.get(i)
										.getSemester()));
					}
				}
				
				teachPlanVOs = teachPlanVOsT;
				
			}
		}
		fillTable();
	}

	public void setMyTabbedPane(MyTabbedPane tabbedPane) {
		this.tabbedPane = tabbedPane;
	}
}
